/**
 * @FileName: Preferences.java
 * @Package:com.bdtop.dbl.utils
 * @Description: TODO
 * @author: MingjunLi
 * @date:2014-9-16
 * @version V1.0
 */

package com.bdtop.dbl.utils;

import java.io.File;
import java.io.InputStream;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.text.TextUtils;
import android.util.Base64;

import com.bdtop.dbl.bluetooth.BleDeviceSettingsActivity;

/**
 * @ClassName: Preferences
 * @Description: TODO
 * @author: MingjunLi
 * @date:2014-9-16
 */
public class Preferences_back {

	// Preference key
	private static final String IS_FIRST_RUNNING = "is_first_running";
	public static final String REMEMBER_PWD = "remember_pwd";
	public static final String AUTO_LOGIN = "auto_login";
	public static final String REMEMBERED_USERNAME = "remembered_username";
	public static final String REMEMBERED_PWD = "remembered_pwd";
	public static final String LOGIN_SIGN = "login_sign";
	public static final String SELF_POSITION = "self_position";
	public static final String SELF_CITY_CODE = "self_city_code";
	public static final String SELF_LOCATION_INFO = "self_location_info";

	// private static final String VISITOR_MODE = "visitor_Mode";
	public static final String CURRENT_BLE_MAC = "current_ble_mac";
	public static final String CURRENT_BLE_NAME = "current_ble_name";

	// private static final String CURRENT_BLE_DISTANCE =
	// "current_ble_distance";
	// private static final String CURRENT_BLE_ALARM_TIME =
	// "current_ble_alarm_time";
	private static final String RESTART_BLE_NAME_AFTER_EXIT_APP = "restart_ble_name_After_exit_app";
	private static final String USER_IMG = "u";
	// private static final String USER_IMG_NAME = "user_img_name";
	private static final String DEVICE_IMG = "d";
	// private static final String DEVICE_IMG_NAME = "device_img_name";
	public static final String VERSION = "version";
	public static final String DOWNURL = "downurl";
	// private static final String SHOULD_REMIND_DOWN_MAP =
	// "should_remind_down_map";
	public static final String ISLOGIN = "ISLOGIN";
	public static final String FROM_PUSH = "from_push";
	private static final String IS_BIND_CID = "is_bind_cid";
	public static final String LAST_LOGIN_USER = "last_login_user";

	private static final String IMEI_FOR_MAC = "imei_for_mac_";
	private static final String MAC_FOR_IMEI = "mac_for_imei_";
	private static final String REBIND_IMEI = "rebind_imei";

	private static final String REMIND_DOWN_MAP_TIME = "remind_down_map_time";

	private static final String REGSITER_TO_LOGIN = "regsiter_to_login";

	public static SharedPreferences getPreferences(Context context) {
//		return PreferenceManager.getDefaultSharedPreferences(context);
		return context.getSharedPreferences(Constants.SP_PATH,  Context.MODE_PRIVATE);
	}

	/**
	 * Get the first running flag from shared preferences, true is default.
	 * 
	 * @param @param context
	 * @param @return
	 * @return boolean
	 * @throws
	 */
	public static boolean isFirstRunning(Context context) {
		return getPreferences(context).getBoolean(IS_FIRST_RUNNING, true);
	}

	/**
	 * Set the first running flag into shared preferences.
	 * 
	 * @param @param context
	 * @param @param isFirstRunning
	 * @return void
	 * @throws
	 */
	public static void setFirstRunning(Context context, boolean isFirstRunning) {
		getPreferences(context).edit().putBoolean(IS_FIRST_RUNNING, isFirstRunning).commit();
	}

	// Save & Get remember password flag.
	public static boolean isRememberPwd(Context context) {
		return getPreferences(context).getBoolean(REMEMBER_PWD, false);
	}

	public static void setRememberPwd(Context context, boolean rememberPwd) {
		getPreferences(context).edit().putBoolean(REMEMBER_PWD, rememberPwd).commit();
	}

	// Save & Get auto login flag.
	public static boolean isAutoLogin(Context context) {
		return getPreferences(context).getBoolean(AUTO_LOGIN, false);
	}

	public static void setAutoLogin(Context context, boolean autoLogin) {
		getPreferences(context).edit().putBoolean(AUTO_LOGIN, autoLogin).commit();
	}

	// Save & Get the password by user.
	public static void storePwdByUser(Context context, String userName, String pwd) {
		getPreferences(context).edit().putString(userName, pwd).commit();
	}

	public static String getPwdByUser(Context context, String userName) {
		return getPreferences(context).getString(userName, "");
	}

	// Save & Get user name(phone number).
	public static String getUserName(Context context) {
		return getPreferences(context).getString(REMEMBERED_USERNAME, "");
	}

	public static void saveUserName(Context context, String userName) {
		getPreferences(context).edit().putString(REMEMBERED_USERNAME, userName).commit();
	}

	// Save & Get password.
	public static String getPassword(Context context) {
		String pwd = getPreferences(context).getString(REMEMBERED_PWD, "");
//		pwd = new String(Base64.decode(pwd.getBytes(), Base64.DEFAULT));
		return pwd;
	}

	public static void savePassword(Context context, String pwd) {
//		pwd = Base64.encodeToString(pwd.getBytes(), Base64.DEFAULT);
		getPreferences(context).edit().putString(REMEMBERED_PWD, pwd).commit();
	}

	// Save & Get login sign.
	public static String getLoginSign(Context context) {
		return getPreferences(context).getString(LOGIN_SIGN, "");
	}

	public static void saveLoginSign(Context context, String loginSign) {
		getPreferences(context).edit().putString(LOGIN_SIGN, loginSign).commit();

		// saveVisitorMode(context, false);
	}

	public static void saveLastSelfLocation(Context context, String[] lat_lng) {
		getPreferences(context).edit().putString(SELF_POSITION, lat_lng[0] + "#" + lat_lng[1]).commit();
	}

	public static String[] getLastSelfLocation(Context context) {
		String latlng = getPreferences(context).getString(SELF_POSITION, "");
		if (TextUtils.isEmpty(latlng))
			return null;

		return new String[] { latlng.split("#")[0], latlng.split("#")[1] };
	}

	public static void saveLocationInfo(Context context, String locationInfo) {
		getPreferences(context).edit().putString(SELF_LOCATION_INFO, locationInfo).commit();
	}

	public static String getLocationInfo(Context context) {
		return getPreferences(context).getString(SELF_LOCATION_INFO, "");
	}

	public static void saveLastCityCode(Context context, String cityCode) {
		getPreferences(context).edit().putString(SELF_CITY_CODE, cityCode).commit();
	}

	public static String getLastCityCode(Context context) {
		return getPreferences(context).getString(SELF_CITY_CODE, "");
	}

	// public static void saveVisitorMode(Context context, boolean
	// isVisitorMode) {
	// getPreferences(context).edit().putBoolean(VISITOR_MODE,
	// isVisitorMode).commit();
	// }
	//
	// public static boolean getVisitorMode(Context context) {
	// return getPreferences(context).getBoolean(VISITOR_MODE, true);
	// }

	public static void saveCurrentBleMac(Context context, String mac) {
		getPreferences(context).edit().putString(CURRENT_BLE_MAC, mac).commit();
	}

	public static String getCurrentBleMac(Context context) {
		return getPreferences(context).getString(CURRENT_BLE_MAC, "");
	}

	public static void saveCurrentBleName(Context context, String name) {
		getPreferences(context).edit().putString(CURRENT_BLE_NAME, name).commit();
	}

	public static String getCurrentBleName(Context context) {
		return getPreferences(context).getString(CURRENT_BLE_NAME, "");
	}

	// public static void saveCurrentBleDistance(Context context, float
	// distance) {
	// String bleName = getCurrentBleName(context);
	// getPreferences(context).edit().putFloat(CURRENT_BLE_DISTANCE + bleName,
	// distance).commit();
	// }
	//
	// public static float getCurrentBleDistance(Context context) {
	// String bleName = getCurrentBleName(context);
	// return getPreferences(context).getFloat(CURRENT_BLE_DISTANCE + bleName,
	// 5f);
	// }
	//
	// public static void saveCurrentBleAlarmTime(Context context, long time) {
	// String bleName = getCurrentBleName(context);
	// getPreferences(context).edit().putLong(CURRENT_BLE_ALARM_TIME + bleName,
	// time).commit();
	// }
	//
	// public static long getCurrentBleAlarmTime(Context context) {
	// String bleName = getCurrentBleName(context);
	// return getPreferences(context).getLong(CURRENT_BLE_ALARM_TIME + bleName,
	// 5000l);
	// }

	public static void saveReStartBleNameAfterAppExit(Context context, String mBluetoothDeviceAddress) {
		getPreferences(context).edit().putString(RESTART_BLE_NAME_AFTER_EXIT_APP, mBluetoothDeviceAddress).commit();
	}

	public static String getReStartBleNameAfterAppExit(Context context) {
		return getPreferences(context).getString(RESTART_BLE_NAME_AFTER_EXIT_APP, "");
	}

	public static void saveUserImg(Context context, String userImgBase64, String username) {
		// getPreferences(context).edit().putString(username + "_" + USER_IMG,
		// userImgBase64).commit();
		// fix : 图片存在指定文件里
		String fileName = username + "_" + USER_IMG + ".txt";
		File file = CacheUtil.getDiskHeadFileDir(context, fileName);
		boolean result = FileUtil.inputStreamToFile(FileUtil.byteToInputStream(userImgBase64.getBytes()), file);
		DBLDebug.logd("save user image :" + (result ? "success" : "fail"));
	}

	public static String getUserImgBase64(Context context, String username) {
		// return getPreferences(context).getString(username + "_" + USER_IMG,
		// "");
		// fix : 图片存在指定文件里
		String fileName = username + "_" + USER_IMG + ".txt";
		File file = CacheUtil.getDiskHeadFileDir(context, fileName);
		String result = FileUtil.FileToString(file);
		DBLDebug.logd("get user image :" + (!TextUtils.isEmpty(result) ? "success" : "no data"));
		return result;
	}

	public static void saveDeviceImg(Context context, String deviceImgBase64, String deviceImei) {
		// getPreferences(context).edit().putString(deviceImei + "_" +
		// DEVICE_IMG, deviceImgBase64).commit();
		// fix : 图片存在指定文件里
		String fileName = deviceImei + "_" + DEVICE_IMG + ".txt";
		File file = CacheUtil.getDiskHeadFileDir(context, fileName);
		boolean result = FileUtil.inputStreamToFile(FileUtil.byteToInputStream(deviceImgBase64.getBytes()), file);
		DBLDebug.logd("save device image :" + (result ? "success" : "fail"));
	}

	public static String getDeviceImgBase64(Context context, String deviceImei) {
		// return getPreferences(context).getString(deviceImei + "_" +
		// DEVICE_IMG, "");
		// fix : 图片存在指定文件里
		String fileName = deviceImei + "_" + DEVICE_IMG + ".txt";
		File file = CacheUtil.getDiskHeadFileDir(context, fileName);
		String result = FileUtil.FileToString(file);
		DBLDebug.logd("get device image :" + (!TextUtils.isEmpty(result) ? "success" : "no data"));
		return result;
	}

	public static void saveVersion(Context context, String version) {
		getPreferences(context).edit().putString(VERSION, version).commit();
	}

	public static String getVersion(Context context) {
		return getPreferences(context).getString(VERSION, "100");
	}

	public static void saveDownurl(Context context, String downurl) {
		getPreferences(context).edit().putString(DOWNURL, downurl).commit();
	}

	public static String getDownurl(Context context) {
		return getPreferences(context).getString(DOWNURL, "");
	}

	public static boolean shouldRemindDownMap(Context context) {
		int time = getPreferences(context).getInt(REMIND_DOWN_MAP_TIME, 0);

		if (time > Constants.REMIND_DOWN_MAP_TIME) {
			// DBLDebug.logd("timetimetimetimetimetime!!!!" + time);
			return false;
		} else {
			// DBLDebug.logd("timetimetimetimetimetimeXXX" + time);
			getPreferences(context).edit().putInt(REMIND_DOWN_MAP_TIME, time + 1).commit();
			return true;
		}
	}

	public static void saveLoginStatus(Context context, boolean isLogin) {
		getPreferences(context).edit().putBoolean(ISLOGIN, isLogin).commit();
	}

	public static boolean isLogin(Context context) {
		return getPreferences(context).getBoolean(ISLOGIN, false);
	}

	public static void saveFromPush(Context context, boolean fromPush) {
		getPreferences(context).edit().putBoolean(FROM_PUSH, fromPush).commit();
	}

	public static boolean isFromPush(Context context) {
		return getPreferences(context).getBoolean(FROM_PUSH, false);
	}

	public static void saveCidStatus(Context context, boolean isCidBind) {
		getPreferences(context).edit().putBoolean(IS_BIND_CID, isCidBind).commit();
	}

	public static boolean isBindCid(Context context) {
		return getPreferences(context).getBoolean(IS_BIND_CID, false);
	}

	public static void saveLastLoginUser(Context context, String lastLoginUser) {
		getPreferences(context).edit().putString(LAST_LOGIN_USER, lastLoginUser).commit();
	}

	public static String getLastLoginUser(Context context) {
		return getPreferences(context).getString(LAST_LOGIN_USER, "");
	}

	public static final String DBL_DEVICE_FANGDIU = "dbl_device_fangdiu_";
	public static final String DBL_DEVICE_FANGDIU_JULI = "dbl_device_fangdiu_juli_";
	public static final String DBL_DEVICE_FANGDIU_DUANKAI = "dbl_device_fangdiu_duankai_";

	public static void saveBleDeviceFangdiu(Context context, String mac, boolean checked) {
		getPreferences(context).edit().putBoolean(DBL_DEVICE_FANGDIU + mac, checked).commit();
	}

	public static boolean getBleDeviceFangdiu(Context context, String mac) {
		return getPreferences(context).getBoolean(DBL_DEVICE_FANGDIU + mac, true);
	}

	public static void saveBleDeviceFangdiuJuli(Context context, String mac, int juli) {
		getPreferences(context).edit().putInt(DBL_DEVICE_FANGDIU_JULI + mac, juli).commit();
	}

	public static int getBleDeviceFangdiuJuli(Context context, String mac) {
		return getPreferences(context).getInt(DBL_DEVICE_FANGDIU_JULI + mac, Constants.DEFAULT_FANGDIU_JULI);
	}

	public static void saveBleDeviceFangdiuDuankai(Context context, String mac, boolean checked) {
		getPreferences(context).edit().putBoolean(DBL_DEVICE_FANGDIU_DUANKAI + mac, checked).commit();
	}

	public static boolean getBleDeviceFangdiuDuankai(Context context, String mac) {
		return getPreferences(context).getBoolean(DBL_DEVICE_FANGDIU_DUANKAI + mac, true);
	}

	public static void saveImeiForMac(Context context, String mac, String imei) {
		getPreferences(context).edit().putString(IMEI_FOR_MAC + mac, imei).commit();

		saveMacForImei(context, imei, mac);
	}

	public static String getImeiForMac(Context context, String mac) {
		return getPreferences(context).getString(IMEI_FOR_MAC + mac, "");
	}

	public static void saveMacForImei(Context context, String imei, String mac) {
		getPreferences(context).edit().putString(MAC_FOR_IMEI + imei, mac).commit();
	}

	public static String getMacForImei(Context context, String imei) {
		return getPreferences(context).getString(MAC_FOR_IMEI + imei, "");
	}

	public static void saveRebindImei(Context context, String imei) {
		getPreferences(context).edit().putString(REBIND_IMEI, imei).commit();
	}

	public static String getRebindImei(Context context) {
		return getPreferences(context).getString(REBIND_IMEI, "");
	}

	public static void saveRegisterToLogin(Context context, boolean flag) {
		getPreferences(context).edit().putBoolean(REGSITER_TO_LOGIN, flag).commit();
	}

	public static boolean getRegisterToLogin(Context context) {
		return getPreferences(context).getBoolean(REGSITER_TO_LOGIN, false);
	}

}
